package controller;

import com.zuche.framework.tradingPlatform.GroupType;
import com.zuche.framework.tradingPlatform.ReplyRequestParams;
import com.zuche.framework.tradingPlatform.RequestParams;
import com.zuche.framework.tradingPlatform.TradingServiceExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pojo.CreditCardConfig;
import pojo.TradingCard;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 张俊亮 (jl.zhang07@zuche.com) on 2015/12/3.
 */
@Controller
public class CardTest {

    private static final String url = "/cardTest.jsp";

    private String token;

    private String recordid;

    private String sourceId;

    private String cardType;

    @RequestMapping("doCheck.do_")
    public String doCheck(HttpServletRequest request,Model model,@RequestParam String cardNo) {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put(RequestParams.GROUP_TYPE, GroupType.QUERY_CARD_INFO);//GroupType.CREATE_NO_SMS
        paramMap.put(RequestParams.CARD_NO,cardNo);
        paramMap.put(RequestParams.SYS_GROUP, "1");
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, false);
        //提取同步回复信息
        CreditCardConfig config = new CreditCardConfig();
        config.setIsExpireTimeNeed(Integer.parseInt(result.get("isExpireTimeNeed")));
        config.setIsCVVNeed(Integer.parseInt(result.get("isCardNoNeed")));
        config.setIsPayerNameNeed(Integer.parseInt(result.get("isPayerNameNeed")));
        config.setIsIdentifyNoNeed(Integer.parseInt(result.get("isIdentifyNoNeed")));
        model.addAttribute("config2Show", config);
        model.addAttribute("cardNo",cardNo);
        sourceId = result.get(ReplyRequestParams.SOURCE_ID);
        cardType = result.get(ReplyRequestParams.CARD_INFO);
//        model.addAttribute("sourceId",result.get(ReplyRequestParams.SOURCE_ID));

        return url;
    }

    @RequestMapping("cardSendSms.do_")
    private String doSendSms(HttpServletRequest request, Model model, TradingCard card) {
//        String sourceId = request.getParameter("sourceId");
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put(RequestParams.GROUP_TYPE, GroupType.CREATE_NO_SMS);
        paramMap.put(RequestParams.CARD_NO, card.getCardNo());
        paramMap.put(RequestParams.PAYER_TEL, card.getMemberTel());
        paramMap.put(RequestParams.IDENTITY_ID, card.getIdentityId());
        paramMap.put(RequestParams.IDENTITY_TYPE, "0");
        paramMap.put(RequestParams.PAYER_NAME, card.getMemberName());
        paramMap.put(RequestParams.SOURCE_ID, sourceId);
        paramMap.put(RequestParams.CARD_TYPE,cardType);
        paramMap.put(RequestParams.BUSINESS_MONEY, "1");
        paramMap.put(RequestParams.PAYER_ID, "5125100");
        paramMap.put(RequestParams.DIRECT_CARD, "1");
        paramMap.put(RequestParams.IS_SCD, "true");
        Map<String, String> result = TradingServiceExecutor.doRpc(paramMap, false);
        recordid = result.get(ReplyRequestParams.RECORD_ID);
        token = result.get(ReplyRequestParams.TOKEN);
        return result.get(ReplyRequestParams.STATUS);
    }

    @RequestMapping("cardBind.do_")
    public String doBind(HttpServletRequest request, Model model,TradingCard card) {
        String validCode = request.getParameter("validCode");
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put(RequestParams.GROUP_TYPE, GroupType.CREATE_NO_SMS);
        paramMap.put(RequestParams.CARD_NO, card.getCardNo());
        paramMap.put(RequestParams.PAYER_TEL, card.getMemberTel());
        paramMap.put(RequestParams.IDENTITY_ID, card.getIdentityId());
        paramMap.put(RequestParams.IDENTITY_TYPE, "0");
        paramMap.put(RequestParams.PAYER_NAME, card.getMemberName());
        paramMap.put(RequestParams.SOURCE_ID, sourceId);
        paramMap.put(RequestParams.CARD_TYPE,cardType);
        paramMap.put(RequestParams.BUSINESS_MONEY, "1");
        paramMap.put(RequestParams.PAYER_ID, "5125100");
        paramMap.put(RequestParams.DIRECT_CARD, "1");
        paramMap.put(RequestParams.IS_SCD, "true");
        paramMap.put(RequestParams.IS_VALIDATE, "true");
        paramMap.put(RequestParams.VALID_CODE, validCode);
        Map<String, String> result = TradingServiceExecutor.doRpc(paramMap, false);
        return result.get(ReplyRequestParams.STATUS);
    }

}

